/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package relenriadweb.devel;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * @author ppikarin
 */
@ManagedBean
@SessionScoped
public class TestController {
     
    /**
     * Creates a new instance of TestController
     */
    
    int startId;
    int endId;
    int testId;
    DataModel testTitles;
    TestHelper helper;
    private int recordCount = 10;
    private int pageSize = 3;
    private Test current;
    private int selectedItemIndex;
    
    public TestController() {
        helper = new TestHelper();
        startId = 1;
        endId = 10;
    }
    
    public TestController(int startId, int endId) {
        helper = new TestHelper();
        this.startId = startId;
        this.endId = endId;
    }
    
    public Test getSelected() {
        if (current == null) {
            current = new Test();
            selectedItemIndex = -1;
        }
        return current;
    }
    
    public DataModel getTestTitles() {
        if (testTitles == null) {
            testTitles = new ListDataModel(helper.getTestTitles(startId, endId));
        }
        return testTitles;
    }
    
    public DataModel getTestText() {
        if (testTitles == null) {
            testTitles = new ListDataModel(helper.getTestText(testId));
        }
        return testTitles;
    }

    void recreateModel() {
        testTitles = null;
    }
    
    /*-------------------------*/
    
    public boolean isHasNextPage() {
        if (endId + pageSize <= recordCount) {
            return true;
        }
        return false;
    }
    
    public boolean isHasPreviousPage() {
        if (startId-pageSize > 0) {
            return true;
        }
        return false;
    }
    
    public String next() {
        startId = endId+1;
        endId = endId + pageSize;
        recreateModel();
        return "index";
    }
    
    public String previous() {
        startId = startId - pageSize;
        endId = endId - pageSize;
        recreateModel();
        return "index";
    }
    
    public int getPageSize() {
        return pageSize;
    } 
    
    
    public String prepareView(){
        current = (Test) getTestTitles().getRowData();
        return "browse";
    }
    public String prepareList(){
        recreateModel();
        return "index";
    }
    
    
    
    
    
}
